/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.driver;

import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingDetailedTO;
import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingEntity;
import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingTO;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;

/**
 *
 * @author Alexandra
 */
@Stateless
public class DriverBean implements DriverRemote {

    private static Log log = LogFactory.getLog(DriverBean.class);
    @PersistenceContext
    private EntityManager em;

    public List<DriverDetailsTO> getAllDrivers() throws DriverException {
        try {
            List<DriverDetailsTO> results = new ArrayList<DriverDetailsTO>();
            Query allDriversQuery = em.createNamedQuery("findAllDrivers");
            List allDrivers = allDriversQuery.getResultList();

            for (int i = 0; i < allDrivers.size(); i++) {
                DriverEntity v = (DriverEntity) allDrivers.get(i);
                results.add(v.getDetailsData());
            }
            return results;
        } catch (Exception ex) {
            log.error("Error in getAllDrivers", ex);
            throw new DriverException("Error in getAllDrivers", ex);
        }
    }

    public DriverDetailsTO getDriverById(Long id) throws DriverException {
        try {
            return em.find(DriverDetailsTO.class, id);
        } catch (Exception ex) {
            log.error("Error in getDriverById " + id, ex);
            throw new DriverException("Error in getDriverById " + id, ex);
        }
    }

    public DriverDetailsTO getDriverByUsername(
            String username) throws DriverException {
        try {
            
            Query query = em.createQuery("SELECT de FROM DriverEntity de WHERE de.userName=:username");
            query.setParameter("username", new String(username));
            List results = query.getResultList();

            if (results != null && !results.isEmpty())
            {
                DriverEntity driver = (DriverEntity) results.get(0);
                return driver.getDetailsData();
            }
            else
                return new DriverDetailsTO();
        } catch (Exception ex) {
            log.error("Error in getDriverByUsername: " + username, ex);
            throw new DriverException("Error in getDriverByUsername: " + username, ex);
        }

    }

    public void updateDriverDetails(DriverDetailsTO to) throws DriverException {
        try {
            em.refresh(to);
        } catch (Exception ex) {
            log.error("Error in updateDriverDetails", ex);
            throw new DriverException("Error in updateDriverDetails", ex);
        }

    }

    public List<VehicleBookingDetailedTO> getDriverJobs(String username) throws DriverException {
        try {
            Query query = em.createQuery("select vb from VehicleBookingEntity vb where vb.driver.userName=:username");

            query.setParameter("username", new String(username));

            List results = query.getResultList();

            List<VehicleBookingDetailedTO> driverJobs = new ArrayList<VehicleBookingDetailedTO>();

            for (int i = 0; i <
                    results.size(); i++) {
                VehicleBookingEntity vehicleBookingEntity = (VehicleBookingEntity) results.get(i);

                VehicleBookingDetailedTO vb = new VehicleBookingDetailedTO();

                vb.setContactEmail(vehicleBookingEntity.getContactEmail());
                vb.setContactName(vehicleBookingEntity.getContactName());
                vb.setContactPhone(vehicleBookingEntity.getContactPhone());
                vb.setDriverId(vehicleBookingEntity.getDriver().getId());
                vb.setDropOffAddress(vehicleBookingEntity.getDropOffAddress());
                vb.setPickUpAddress(vehicleBookingEntity.getPickUpAddress());
                vb.setSpecialRequirement(vehicleBookingEntity.getSpecialRequirement());
                vb.setPickUpDate(vehicleBookingEntity.getPickUpDate());
                vb.setTaskCompleted(vehicleBookingEntity.isTaskCompleted());

                driverJobs.add(vb);
            }

            return driverJobs;
        } catch (Exception ex) {
            log.error("Error in getDriverJobs", ex);
            throw new DriverException("Error in getDriverJobs", ex);
        }

    }

    public void updateDriverJob(Long id,
            boolean taskCompleted) throws DriverException {
        try {
            VehicleBookingTO vehicleBooking = em.find(VehicleBookingTO.class, id);
            vehicleBooking.setTaskCompleted(taskCompleted);

            em.refresh(vehicleBooking);
        } catch (Exception ex) {
            log.error("Error in updateDriverJob", ex);
            throw new DriverException("Error in updateDriverJob", ex);
        }
    }

    public void insertNewDriver(DriverDetailsTO driverDetailsTO) throws DriverException {
        try {
            DriverEntity de = new DriverEntity();

            de.setDetailsData(driverDetailsTO);
            em.persist(de);
        } catch (Exception ex) {
            log.error("Error in insertNewDriver", ex);
            throw new DriverException("Error in insertNewDriver", ex);
        }

    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method" or "Web Service > Add Operation")
}
